﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GRC_TPE_WebApp.Controller;

namespace GRC_TPE_WebApp.View.Private.Communication
{
    public partial class ListCommunications : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Méthode déclenchée au clic un bouton de modification
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Update_button_Click(object sender, EventArgs e)
        {
            String test = (String)((Button)sender).CommandArgument;
            Response.Redirect("EditCommunication.aspx?id=" + test);
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de filtrage
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_Filter_Click(object sender, EventArgs e)
        {
            bool filtered = false;

            ///suppression de tous les paramètres existants
            this.DS_Communications.WhereParameters.Clear();

            ///ajout du filtre sur le titre
            if (!string.IsNullOrEmpty(this.TB_FilterTitre.Text))
            {
                this.DS_Communications.Where += "it.Title LIKE '%"+this.TB_FilterTitre.Text+"%'";
                filtered = true;
            }

            ///ajout du filtre sur la date
            if (this.C_Date.SelectedDate != null && this.C_Date.SelectedDate != DateTime.Parse("01/01/0001 00:00:00"))
            {
                if (filtered)
                    this.DS_Communications.Where += " AND ";
                this.DS_Communications.Where += "it.Date = @CurrentDate";
                this.DS_Communications.WhereParameters.Add(new Parameter() { DbType = System.Data.DbType.DateTime, Name = "CurrentDate", DefaultValue = this.C_Date.SelectedDate.ToString() });
                filtered = true;
            }

            ///ajout du filtre sur le commercial
            if (!string.IsNullOrEmpty(this.TB_FilterUser.Text))
            {
                if (filtered)
                    this.DS_Communications.Where += " AND ";
                this.DS_Communications.Where += "it.User.Lastname LIKE '%" + this.TB_FilterUser.Text + "%'";
                filtered = true;
            }

            ///ajout du filtre sur le client
            if (!string.IsNullOrEmpty(this.TB_FilterClient.Text))
            {
                if(filtered)
                    this.DS_Communications.Where += " AND ";
                this.DS_Communications.Where += "it.Client.Name LIKE '%" + this.TB_FilterClient.Text + "%'";
            }
            
            string test = this.DS_Communications.Where;
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de réinitialisation de filtres
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_Clear_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListCommunications.aspx");
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de suppression
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_Delete_Click(object sender, EventArgs e)
        {
            String ids = (String)((Button)sender).CommandArgument;
            int id = int.Parse(ids);
            if (CommunicationController.DeleteCommunication(id) == "No error")
                Response.Redirect("ListCommunications.aspx");
        }
    }
}